package com.heima.mvplibrary.base;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.heima.mvplibrary.R;
import com.heima.mvplibrary.mvp.IView;
import com.heima.mvplibrary.utils.AppManager;
import com.heima.mvplibrary.utils.StaticUtils;

import java.lang.reflect.ParameterizedType;

import butterknife.ButterKnife;

public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements IView, View.OnClickListener{

    private P p;
    private View emptyView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //隐藏状态栏
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        initData();
        initListener();
        AppManager.getAppManager().addActivity(this);
    }


      /**
     * View点击
     **/
    protected abstract void widgetClick(View v);

    @Override
    public void onBackPressed() {
        //super.onBackPressed();

        //屏蔽掉所以activity的返回按键

    }


    public void showToast(String msg){
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    long isShow = 0;
    public void showLongToast(String msg){
        long data = System.currentTimeMillis();
        Log.i("base", "showToast: "+(data-isShow));
        if (data-isShow<30000)return;
        isShow = data;
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

    /**
     * 获取Presenter
     */
    public P getP(){
        if(p == null){
            //泛型形式实例化P层
            ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
            Class<P> clazz = (Class<P>) parameterizedType.getActualTypeArguments()[0];
            try {
                p = clazz.newInstance();
            }catch (Exception e){
                StaticUtils.loge(e.getMessage());
            }
        }
        if(p != null){
            if(!p.isAttachedV()){
                p.attachView(this);
            }
        }
        return p;
    }

    /**
     * 错误处理
     */
    public void onError(Throwable e){
        showToast(StaticUtils.getErrorMsg(e));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(getP()!=null){
            getP().detachView();
        }
        p = null;
    }

    /**
     * 设置适配器的空布局
     * @param adapter 适配器
     * @param msg 空布局文字提示
     * @param imgResId 空布局图片资源，若isLoad为true则不生效
     * @param isLoad 是否是加载中
     */
    public void setAdapterEmptyView(BaseQuickAdapter adapter, String msg, int imgResId, boolean isLoad){
        if(emptyView == null){
            emptyView = getLayoutInflater().inflate(R.layout.view_empty, null);
        }
        ((TextView)emptyView.findViewById(R.id.textView_msg)).setText(msg);
        if(isLoad){
            emptyView.findViewById(R.id.imageView_img).setVisibility(View.GONE);
            emptyView.findViewById(R.id.progressBar_loading).setVisibility(View.VISIBLE);
        }else{
            ((ImageView)emptyView.findViewById(R.id.imageView_img)).setImageResource(imgResId);
            emptyView.findViewById(R.id.imageView_img).setVisibility(View.VISIBLE);
            emptyView.findViewById(R.id.progressBar_loading).setVisibility(View.GONE);
        }
        adapter.getData().clear();
        adapter.setEmptyView(emptyView);
        adapter.notifyDataSetChanged();
    }


    /**
     * 显示错误布局
     *
     * @param adapter recyclerView的适配器
     * @param msg     错误提示信息
     */
    public void showErrorView(BaseQuickAdapter adapter, String msg) {
        //// TODO: 2019/6/5 替换错误布局图片
        setAdapterEmptyView(adapter, msg, R.drawable.loading_error, false);
    }

    /**
     * 显示空布局
     *
     * @param adapter recyclerView的适配器
     */
    public void showEmptyView(BaseQuickAdapter adapter) {
        //// TODO: 2019/6/5 替换空布局图片
        setAdapterEmptyView(adapter, "空空如也", R.drawable.loading_null, false);
    }

    /**
     * 显示加载中布局
     *
     * @param adapter recyclerView的适配器
     */
    public void showLoadView(BaseQuickAdapter adapter) {
        setAdapterEmptyView(adapter, "加载中……", 0, true);
    }


    /**
     * @param clazz 启动activity
     */
    public void start(@NonNull Class<? extends AppCompatActivity> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        if (fastClick())
            widgetClick(v);

    }

    /**
     * [防止快速点击]
     *
     * @return
     */
    private boolean fastClick() {
        long lastClick = 0;
        if (System.currentTimeMillis() - lastClick <= 1000) {
            return false;
        }
        lastClick = System.currentTimeMillis();
        return true;
    }

}
